<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Vidracaria</title>
    </head>
    <body>
        <div class="total">
            <div class="cabecalho">
                <?php
                include_once 'titulo.php';
                include_once 'cabecalho.php';
                ?>
            </div>

            <?php
                include_once 'menuHorizontal.php';
            ?>
                <div class="conteudo">
                    <div class="subconteudo">

                    <?php
                    if (empty($_POST) || $_POST['ACAO'] == 'inserirCarro') {
                        inserirCarro();
                    }
                    ?>
                    <?php
                    if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'pesquisaCarro') {
                        pesquisaCarro($_POST['PESQUISA']);
                    }
                    ?>
                    <?php
                    //Só aparece a caixa de edição se houver um post com a ação=editarUsuario
                    if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'updateCarro') {
                        updateCarro();
                    }
                    ?>

                    <div style="width:90%; margin-left:auto; margin-right: auto;">
                        <form action="cadastro_carro.php" method="post">
                            <input type="hidden" name="ACAO" value="inserirCarro" />
                            <input type="submit" value="Novo Carro" class="btnAdicionar"/>
                        </form>
                    </div>
                    <?php

                    function inserirCarro() {
                    ?>
                        <form action="cadastro_carro.php" method="POST">
                            <table class="tblPesquisar"><tr><td>
                                        <label class="label3">Procurar Carro:</label>
                                        <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                                        <input type="hidden" name="ACAO" value="pesquisaCarro" />
                                        <input type="submit" value="" class="buttonPesquisar"/>
                            </table>
                        </form><br/><br/>

                        <table class="tblForm">
                            <form action="script/acoes.php" method="post">
                                <input type="hidden" name="ACAO" value="inserirCarro"/>
                                <thead>
                                <td colspan="2">
                                    <center>Cadastro de Carro</center></td>
                                </thead>
                                <tbody>
                                    <tr><td colspan="2">
                                            <center>Preencha os campos abaixo para cadastrar Carro.</br>
                                                Os campos com * são obrigatórios.</center><br/></td></tr>

                                    <tr><td>
                                            <label class="label">* Nome do Carro</label></td><td>
                                            <input type = "text" name= "NOME" size="30px" maxlength="30"/></td></tr>

                                    <tr><td>
                                            <label class="label">* Placa:</label></td><td>
                                            <input type = "text" name= "PLACA" size="30px" maxlength="7"/></td></tr>

                                    <tr><td>
                                            <label class="label">* Renavan:</label></td><td>
                                            <input type = "text" name= "RENAVAN" size="30px" maxlength="9"/></td></tr>

                                    <tr><td>
                                            <label class="label">* Ano:</label></td><td>
                                            <input type = "text" name= "ANO" size="15px" maxlength="4"/></td></tr>

                                    <tr><td>
                                            <label class="label">* Combustivel</label></td><td>
                                            <input type = "radio" name= "COMBUSTIVEL" value="G"/> Gasolina
                                            <input type = "radio" name= "COMBUSTIVEL" value="A"/> Alcool
                                            <input type = "radio" name= "COMBUSTIVEL" value="D"/> Diesel
                                            <input type = "radio" name= "COMBUSTIVEL" value="S"/> Gás</td></tr>
                                </tbody>
                                <tfoot>
                                    <tr> <td colspan="2">
                                            <input type="submit" value="CADASTRAR" class="blackButtonYes"/>
                                            <input type="reset" value="LIMPAR" class="blackButtonNo"/>
                                        </td></tr>
                                </tfoot>
                            </form>
                        </table>
                    <?php
                    }
                    ?>

                    <?php

                    function updateCarro() {
                    ?>
                        <form action="cadastro_carro.php" method="POST">
                            <table class="tblPesquisar"><tr><td>
                                        <label class="label3">Procurar Carro:</label>
                                        <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                                        <input type="hidden" name="ACAO" value="pesquisarCarro" />
                                        <input type="submit" value="" class="buttonPesquisar"/>
                            </table>
                        </form><br/><br/>
                        <table class="tblForm">
                            <form action="script/acoes.php" method="post">
                                <input type="hidden" name="ACAO" value="updateCarro"/>
                                <thead>
                                <td colspan="2">
                                    <center>Cadastro de Carro</center></td>
                                </thead>
                                <tbody>
                                    <tr><td colspan="2">
                                            <center>Preencha os campos abaixo para cadastrar Carro.</br>
                                                Os campos com * são obrigatórios.</center><br/></td></tr>
                                    <tr><td>
                                            <label class="label">* Nome do Carro</label></td><td>
                                            <input type="hidden" name="ID"
                                        <?php
                                        if ($_POST['ID']) {
                                            echo " value=\"";
                                            echo $_POST['ID'];
                                            echo "\" ";
                                        }
                                        ?>
                                               />
                                        <input type = "text" name= "NOME" size="30px"
                                        <?php
                                        if ($_POST['NOME']) {
                                            echo " value=\"";
                                            echo $_POST['NOME'];
                                            echo "\" ";
                                        }
                                        ?>/></td></tr>

                                <tr><td>
                                        <label class="label">* Placa:</label></td><td>
                                        <input type = "text" name= "PLACA" size="30px"
                                        <?php
                                        if ($_POST['PLACA']) {
                                            echo " value=\"";
                                            echo $_POST['PLACA'];
                                            echo "\" ";
                                        }
                                        ?>/></td></tr>

                                <tr><td>
                                        <label class="label">* Renavan:</label></td><td>
                                        <input type = "text" name= "RENAVAN" size="30px"
                                        <?php
                                        if ($_POST['RENAVAN']) {
                                            echo " value=\"";
                                            echo $_POST['RENAVAN'];
                                            echo "\" ";
                                        }
                                        ?>/></td></tr>
                                <tr><td>
                                        <label class="label">* Ano:</label></td><td>
                                        <input type = "text" name= "ANO" size="15px"
                                        <?php
                                        if ($_POST['ANO']) {
                                            echo " value=\"";
                                            echo $_POST['ANO'];
                                            echo "\" ";
                                        }
                                        ?>/></td></tr>

                                <tr><td>
                                        <label class="label">* Combustivel</label></td><td>
                                        <?php
                                        if ($_POST['COMBUSTIVEL'] == "G") {
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"G\" checked/> Gasolina";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"A\"/> Alcool";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"D\"/> Diesel";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"S\"/> Gás</td></tr>";
                                        } elseif ($_POST['COMBUSTIVEL'] == "A") {
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"G\"/> Gasolina";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"A\" checked/> Alcool";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"D\"/> Diesel";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"S\"/> Gás</td></tr>";
                                        } elseif ($_POST['COMBUSTIVEL'] == "D") {
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"G\"/> Gasolina";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"A\"/> Alcool";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"D\" checked/> Diesel";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"S\"/> Gás</td></tr>";
                                        } elseif ($_POST['COMBUSTIVEL'] == "S") {
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"G\"/> Gasolina";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"A\"/> Alcool";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"D\"/> Diesel";
                                            echo "<input type = \"radio\" name= \"COMBUSTIVEL\" value=\"S\" checked/> Gás</td></tr>";
                                        }
                                        ?>
                            </tbody>
                            <tfoot>
                            <td colspan="2">
                                <input type="submit" value="CADASTRAR" class="btnAdicionar"/>
                                <input type="reset" value="LIMPAR" class="btnLimpar"/>
                                <form action="cadastro_cliente.php" method="post"> <!--Se eu clicar aqui é para cancelar o novo usuários e mostrar a lista-->
                                    <input type="submit" value="CANCELAR" class="btnVoltar"/>
                                </form>
                            <tfoot/>
                        </form>
                    </table>
                    <?php
                                    }
                    ?>

                    <?php

                                    function pesquisaCarro($pesquisa) {
                                        echo "<div style='width:98%; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto;'>";
                                        include("script/conecta.php");
                                        conecta();
                                        $resultado = mysql_query("SELECT * FROM tbl_carro WHERE car_nome LIKE '%" . $pesquisa . "%' ORDER BY car_id;");

                                        //4-Consome o resultado. Se tiver
                                        echo "<table border='1' class='tblUsuarios'>"; //crio uma tabela para exibição de dados
                                        echo "<caption>Tabela de Clientes.</caption>";

                                        echo "<thead>";
                                        echo "<th>" . "ID" . "</th>";
                                        echo "<th>" . "Carro" . "</th>";
                                        echo "<th>" . "Placa" . "</th>";
                                        echo "<th>" . "Renavan" . "</th>";
                                        echo "<th>" . "Ano" . "</th>";
                                        echo "<th>" . "Combustível" . "</th>";
                                        echo "<th>" . "Ações" . "</th>";
                                        echo "</thead>";

                                        echo "<tbody>";

                                        while ($linha = mysql_fetch_array($resultado)) {
                                            echo "<tr>";

                                            echo "<td style='text-align:center;'>" . $linha["car_id"] . "</td>";
                                            echo "<td>" . $linha["car_nome"] . "</td>";
                                            echo "<td>" . $linha["car_placa"] . "</td>";
                                            echo "<td>" . $linha["car_renavan"] . "</td>";
                                            echo "<td>" . $linha["car_ano"] . "</td>";

                                            echo "<td>";
                                            if ($linha["car_combustivel"] == "G") {
                                                echo "GASOLINA";
                                            } elseif ($linha["car_combustivel"] == "A") {
                                                echo "ALCOOL";
                                            } elseif ($linha["car_combustivel"] == "D") {
                                                echo "DIESEL";
                                            } elseif ($linha["car_combustivel"] == "S") {
                                                echo "GÁS";
                                            }
                                            echo "</td><td>";

                                            echo "<form method='post' action='script/acoes.php'>"; //form de excluir
                                            echo "<input type='submit' value='' class='btnExcluir'/>"; //botão de excluir
                                            echo "<input type='hidden'  name='ACAO' value='deleteCarro'/>";
                                            echo "<input type='hidden' name='ID' value='" . $linha["car_id"] . "'/>";
                                            echo "</form>";

                                            echo "<form method='post' action='cadastro_carro.php'>"; //form de excluir
                                            echo "<input type='submit' value='' class='btnEditar' />"; //botão de editar
                                            echo "<input type='hidden' name='ACAO' value='updateCarro'/>";
                                            echo "<input type='hidden' name='ID' value='" . $linha["car_id"] . "'/>";
                                            echo "<input type='hidden' name='NOME' value='" . $linha["car_nome"] . "'/>";
                                            echo "<input type='hidden' name='PLACA' value='" . $linha["car_placa"] . "'/>";
                                            echo "<input type='hidden' name='RENAVAN' value='" . $linha["car_renavan"] . "'/>";
                                            echo "<input type='hidden' name='ANO' value='" . $linha["car_ano"] . "'/>";
                                            echo "<input type='hidden' name='COMBUSTIVEL' value='" . $linha["car_combustivel"] . "'/>";
                                            echo "</form>";

                                            echo "</td>";
                                            echo "</tr>";
                                        }
                                        echo "</tbody>";

                                        echo "<tfoot>";
                                        echo "</tfoot>";

                                        echo "</table>"; //finalizo tabela para exibição de dados
                                        desconecta(); //5 - encerro a conexao
                                    }
                    ?>

                                </div>
                            </div>


                            <div class="rodape">
                <?php
                                    include_once 'rodape.php';
                ?>
            </div>
        </div>

    </body>
</html>
